package cn.terys.terystransactionclient;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import cn.emagsoftware.ui.ToastManager;
import cn.emagsoftware.ui.dialog.DialogManager;
import cn.emagsoftware.ui.staticmanager.StaticManager;
import cn.emagsoftware.util.StringUtilities;
import cn.terys.terystransactionclient.data.NetManager;
import cn.terys.terystransactionclient.data.db.DBManager;
import cn.terys.terystransactionclient.zxing.CaptureActivity;

public class BarcodeConfirmActivity extends BaseActivity
{

    public static final String INTENT_EXTRANAME_VERIFY_INFO = "VERIFY_INFO";

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        if (!WelcomeActivity.IS_APP_RUNNING)
        {
            StaticManager.restartAppWhenDalvikRecycled(this, WelcomeActivity.class);
            return;
        }
        setContentView(R.layout.barcodeconfirm);
        setTitle(R.string.barcodeconfirm_title);

        Intent intent = getIntent();
        String barcodeStr = intent.getStringExtra(CaptureActivity.INTENT_EXTRANAME_BARCODE);
        String codeStr = intent.getStringExtra(CaptureActivity.INTENT_EXTRANAME_INFO_CODE);
        String nameStr = intent.getStringExtra(CaptureActivity.INTENT_EXTRANAME_INFO_NAME);
        String priceStr = intent.getStringExtra(CaptureActivity.INTENT_EXTRANAME_INFO_PRICE);
        String shopStr = intent.getStringExtra(CaptureActivity.INTENT_EXTRANAME_INFO_SHOP);
        TextView barcode = (TextView) findViewById(R.id.barcode);
        if (barcodeStr == null)
        {
            barcode.setText(codeStr);
            TextView name = (TextView) findViewById(R.id.name);
            name.setText(nameStr);
            TextView price = (TextView) findViewById(R.id.price);
            price.setText(Html.fromHtml("<font color='red'><b>" + priceStr + "</b></font>&nbsp;&nbsp;" + getString(R.string.generic_sum_rmb_unit)));
            TextView shop = (TextView) findViewById(R.id.shop);
            shop.setText(shopStr);
        } else
        {
            barcode.setText(barcodeStr);
            findViewById(R.id.nameWrap).setVisibility(View.GONE);
            findViewById(R.id.priceWrap).setVisibility(View.GONE);
            findViewById(R.id.shopWrap).setVisibility(View.GONE);
        }

        final String unifiedBarcode = barcodeStr == null ? codeStr : barcodeStr;
        Button confirm = (Button) findViewById(R.id.confirm);
        confirm.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                String batchMaxTransaction = WelcomeActivity.APP_PARAM.getParameter("BatchMaxTransaction");
                if (StringUtilities.isAllCharDigit(batchMaxTransaction))
                {
                    long batchMax = Long.parseLong(batchMaxTransaction);
                    if (DBManager.getBatchTransCountFromVerify(BarcodeConfirmActivity.this, MainActivity.BATCH_NUM) + 1 > batchMax)
                    {
                        ToastManager.showLong(BarcodeConfirmActivity.this, StringUtilities.replaceWordsAll(getString(R.string.barcodeconfirm_confirm_transcountout), "{0}", String.valueOf(batchMax)));
                        return;
                    }
                }
                new GenericAsyncTask<String, Object, String[]>(BarcodeConfirmActivity.this)
                {
                    @Override
                    protected String[] doInBackgroundImpl(String... arg0) throws Exception
                    {
                        // TODO Auto-generated method stub
                        return NetManager.barcodeVerify(arg0[0]);
                    }

                    protected void onPostExecute(Object[] objs, final String[] result)
                    {
                        // TODO Auto-generated method stub
                        super.onPostExecute(objs, result);
                        final Context context = (Context) objs[0];
                        DialogManager.showAlertDialog(context, R.string.generic_dialog_title, R.string.barcodeconfirm_confirm_success, new int[] { R.string.generic_dialog_btn_ok }, null, true, false)
                                .setOnDismissListener(new DialogInterface.OnDismissListener()
                                {
                                    @Override
                                    public void onDismiss(DialogInterface dialog)
                                    {
                                        // TODO Auto-generated method stub
                                        Intent intent = new Intent(context, BarcodeVerifyActivity.class);
                                        intent.putExtra(INTENT_EXTRANAME_VERIFY_INFO, result);
                                        context.startActivity(intent);
                                        ((Activity) context).finish();
                                    }
                                });
                    }
                }.execute(unifiedBarcode);
            }
        });
        Button cancel = (Button) findViewById(R.id.cancel);
        cancel.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                finish();
            }
        });

    }

}